#!/bin/bash

#路径
CentOS_NETWORK_DIR="/etc/sysconfig/network-scripts/";
Ubuntu_NETWORK_DIR="/etc/network/"

DISTRO=`./01_Get_Dist_Name Get_Dist_Name`
hostname=client1
version=`cat /etc/redhat-release | awk '{print substr($4,1,1)}'`

#下载必要工具
if [ "$DISTRO" == "CentOS" -a "$version" == "7" ];then
	yum -y install redhat-lsb-core-4.1-27.el7.centos.1.x86_64 wget ntp openssh-server lrzsz vim 
elif [ "$DISTRO" == "CentOS" -a "$version" == "6" ];then 
	yum -y install redhat-lsb-core-4.1-27.el6.centos.1.x86_64 wget ntp openssh-server lrzsz vim
else
	apt-get -y install ntp openssh-server 
fi

#01 配置主机名
init_Hostname(){
    #host命令修改
	if [ $DISTRO == "CentOS" -a $version == "7" ];then
        	if [ "$host" == "$hostname" ];then
			echo "主机名和所制定的更改的主机名一致，不必更新"
		else
			hostnamectl set-hostname $hostname
			echo "CentOS7 主机名更新成功" 
		fi
	elif [ $DISTRO == "CentOS" -a $version == "6" ];then
		if [ "$host" == "$hostname" ];then
                        echo "主机名和所制定的更改的主机名一致，不必更新"
                else
			hostname $hostname
                	echo "HOSTNAME=$hostname" >> /etc/sysconfig/network
               		echo "NETWORKING=yes" >> /etc/sysconfig/network
                	echo "$hostname" > /etc/hostname   #覆盖
                	echo "CentOS6 主机名更新成功"
                fi
	elif [ $DISTRO =="Ubuntu" -a $version == "14" ];then
		if [ "$host" == "$hostname" ];then
                        echo "主机名和所制定的更改的主机名一致，不必更新"
                else
                	echo "$hostname" > /etc/hostname
                	echo "127.0.1.1 $hostname" >> /etc/hosts
                	echo "Ubuntu 主机名更新成功"
		fi
	fi
}
init_Hostname
#02yum源设置 设置为网易163源
yum_source(){
        if [ ${version} == "7" -a $DISTRO == "CentOS" ];then
		
			if [ -f "/etc/yum.repos.d/CentOS7-Base-163.repo" ];then
				echo "CentOS7 yum源已存在"
			else
				#备份
				mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
				#切换到yum源配置文件夹
				cd /etc/yum.repos.d/
				wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
				echo "CentOS7 yum源配置成功"
				#清除以前使用yum源缓存
				yum clean all
				#建立缓存
				yum makecache			
			fi
        elif [ ${version} == "6" -a $DISTRO == "CentOS" ];then
			if [ -f "/etc/yum.repo.d/CentOS6-Base-163.repo" ];then
					echo "CentOS6 yum源已存在"
			else 
				cd /etc/yum.repos.d/
				#将yum 网络源配置文件改名为CentOS-Base.repo.bak，否则会先在网络源中寻找适合的包，改名之后直接从本地源读取。
				mv CentOS-Base.repo CentOS-Base.repo.bak
				wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
				#把下载下来的文件CentOS-Base-163.repo设置为默认源
				mv CentOS6-Base-163.repo CentOS-Base.repo
				echo "CentOS6 yum源配置成功"
				#清除以前使用yum源缓存
				yum clean all
				#建立缓存
				yum makecache
			fi
		elif [ ${version} == "14" -a $DISTRO == "Ubuntu" ];then
				cd /etc/apt
				cp sources.list	sources.list.backup
				echo "" > sources.list
				apt-get update
		fi
}        
yum_source
